package com.jdjw.modules.busfarmersapply.service.impl;

import com.jdjw.common.ResponseResult;
import com.jdjw.modules.busfarmersapply.entity.BusFarmersApply;
import com.jdjw.modules.busfarmersapply.entity.BusFarmersApplyVo;
import com.jdjw.modules.busfarmersapply.mapper.BusFarmersApplyMapper;
import com.jdjw.modules.busfarmersapply.service.BusFarmersApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.modules.sysdept.entity.SysDept;
import com.jdjw.modules.sysdept.service.SysDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 农户重大疾病困难补助 服务实现类
 * </p>
 *
 * @author fq
 * @since 2021-07-23
 */
@Service
public class BusFarmersApplyServiceImpl extends ServiceImpl<BusFarmersApplyMapper, BusFarmersApply> implements BusFarmersApplyService {

    @Resource
    private BusFarmersApplyMapper busFarmersApplyMapper;

    @Resource
    private SysDeptService sysDeptService;

    @Override
    public ResponseResult queryAll(BusFarmersApplyVo busFarmersApplyVo) {
        ResponseResult rrt=new ResponseResult();
        try {
            //拿到数据起始位置
            int start=busFarmersApplyVo.getPage()*busFarmersApplyVo.getRow()-busFarmersApplyVo.getRow();
            busFarmersApplyVo.setPage(start);
            List<BusFarmersApply> busFarmersApplies = busFarmersApplyMapper.queryAll(busFarmersApplyVo);
            for (BusFarmersApply apply : busFarmersApplies) {
                SysDept dept = sysDeptService.getById(apply.getOrgCode());
                if(dept != null){
                    apply.setOrgCodeName(dept.getName());
                }
                    apply.setUserName(busFarmersApplyVo.getUserName());
            }

            if("CJSQ".equals(busFarmersApplyVo.getNodeLogo())){
                for (BusFarmersApply apply : busFarmersApplies) {
                    if("3".equals(apply.getStatus())){
                        apply.setStatus("6");
                    }
                }
            }
            rrt.setMsg("查询成功");
            rrt.setCode(200);
            rrt.setData(busFarmersApplies);
        } catch (Exception e) {
            e.printStackTrace();
            rrt.setMsg("查询成功");
            rrt.setCode(200);
            rrt.setData(null);
        }
        return rrt;
    }

    @Override
    public ResponseResult sqlEdit(String sqlEdit) {
        ResponseResult rrt=new ResponseResult();
        Boolean aBoolean = busFarmersApplyMapper.sqlEdit(sqlEdit);
        if(aBoolean){
            rrt.setMsg("编辑成功");
            rrt.setCode(200);
        }else{
            rrt.setMsg("编辑失败");
            rrt.setCode(500);
        }
        return rrt;
    }
}
